package com.huixue.cn.ztej.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huixue.cn.ztej.bean.Examine;
import com.huixue.cn.ztej.bean.Resource;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.service.SysResourceService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.annotation.RequestMappingName;

@Controller
@RequestMapping(value = "/sys/resource")
public class ResourceController extends BaseController{

	@Autowired
	private SysResourceService sysResourceService;
	
	@RequestMapping("/list")
	public String list(HttpServletResponse response,HttpServletRequest request,Model model){
		List<Resource> list = sysResourceService.findall();
		model.addAttribute("list",list);
		return "/resource/list";
	}
	
	
	@RequestMappingName(value = "跳转到添加页面")
	@RequestMapping(value = "/toAdd/{parentId}", method = RequestMethod.GET)
	public String toAdd(Model model,@PathVariable(value="parentId") String parentId) {
		//System.out.println("parentId============"+parentId);
		if(!parentId.equals("-1")){
			Resource resource = sysResourceService.selectById(parentId);
			model.addAttribute("parentname",resource.getResName());
		}else{
			model.addAttribute("parentname","无");
		}
		model.addAttribute("parentId",parentId);
		return "/resource/add";
		
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Results save(HttpServletResponse response,HttpServletRequest request,@RequestBody Resource resource){
		Results results = null;
		sysResourceService.insert(resource);
		results = new Results(API_RETURN_STATUS.NORMAL.value(), "菜单保存成功！");
		return results; 
	}
	
	@RequestMappingName(value = "跳转到修改页面")
	@RequestMapping(value = "/toUpdate/{resourseId}")
	public String toUpdate(Model model,@PathVariable(value="resourseId") String resourseId) {
		Resource resouce = sysResourceService.selectById(resourseId);
		model.addAttribute("resouce",resouce);
		List<Resource> list = sysResourceService.findParent();
		model.addAttribute("list",list);
		return "/resource/update";
	}
	
	@RequestMapping(value = "/update/{resourceId}",method = RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public Results update(Model model,@PathVariable(value="resourceId") String resourseId,@RequestBody Resource resource) {
		sysResourceService.update(resource);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}
	
	@RequestMapping(value = "/delete/{resourceId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Results delete(Model model,@PathVariable(value="resourceId") String resourseId) {
		sysResourceService.delete(Integer.valueOf(resourseId));
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "删除成功！");
		return results;
	}
	
}
